<?php

class BWCompanyInfoCooker extends BAEventCooker {
    /**
     * 展示类方法
     */

    /**
     * 实现跳转到公司展示的首页
     */
    public function onShowHome() {
        $eventId = $this->getCurrentEvent()->getId();
        $this->updateControl->addRenderView('home', 'client', array('eventId' => $eventId));
    }

    /**
     * 关于公司
     */
    public function onShowCompanyInfo() {
        //获取当前event下面的companyactivity中identify为newslist的list，循环展示出来
        $event = $this->getCurrentEvent();
        $activityTable = new BASheet('companyActivity',$event);
        
        //获取companyactivity这个table中identify为companyinfo的list
        $infoActivityView = $activityTable->getView(array('itemData'=>array('identity' => 'companyinfo')));
        
        //companycontent的“公司介绍”的list
        $companyList = NULL;
        //companycontent的团队介绍的list
        $teamListArr = array();
        foreach ($infoActivityView as $infoActivity) {
            $contentTable = new BASheet('companyContent', $infoActivity);
            $contentView = $contentTable->getView();
            foreach ($contentView as $contentList) {
                $contentName = $contentList->getItem('name')->getValue();
                if(strpos($contentName, '公司介绍') === FALSE) {
                    array_push($teamListArr, $contentList);
                } else {
                    //如果添加了多个“公司介绍”，最后一个会覆盖前面的
                    $companyList = $contentList;
                }
            }
        }
        
        $this->updateControl->addRenderView('showView', 'about', array('companyList' => $companyList, 'teamListArr' => $teamListArr));
    }

    /**
     * 公司动态 列表
     */
    public function onShowTrendsList() {
        //获取当前event下面的companyactivity中identify为newslist的list，循环展示出来
        $event = $this->getCurrentEvent();//获取当前的event
        $activityTable = new BASheet('companyActivity',$event);//获取companyActivity这一层次

        //获取companyactivity这个table中identify为newslist的list
        $newsActivityView = $activityTable->getView(array('itemData'=>array('identity' => 'newslist')));

        //之前的操作是为了找到相应的表，下面的操作是为了获得相应表中的数据
        //用于存储表中identify为newslist的activity的companycontent
        $newsListArr = array();
        foreach ($newsActivityView as $newsActivity) {
            $contentTable = new BASheet('companyContent', $newsActivity);
            $contentView = $contentTable->getView();
            $newsListArr = array_merge($newsListArr, $contentView);
        }
        //限制条数
        $newsListArr = array_slice($newsListArr, 0, 10);
        
        $this->updateControl->addRenderView('showView', 'news_list', array('newsListArr' => $newsListArr));
    }

    /**
     * 根据文章的id获取文章的内容
     */
    public function onShowArticle() {
        $newsId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('newsId');
        $backCooker = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('backCooker');
        $contentTable = new BASheet('companyContent');
        $newsList = $contentTable->getListById($newsId);
        $this->updateControl->addRenderView('showView', 'news_article', array('newsList' => $newsList, 'backCooker' => $backCooker));
    }

    /**
     * 联系我们
     */
    public function onShowContactUs() {
        //获取当前event下面的companyactivity中identify为contactus的list，循环展示出来
        $event = $this->getCurrentEvent();
        $activityTable = new BASheet('companyActivity',$event);

        //identify为contactus的list
        $contactActivityView = $activityTable->getView(array('itemData'=>array('identity' => 'contactus')));

        //存储contactus的companycontent
        $contactList = NULL;
        foreach ($contactActivityView as $contactActivity) {
            $contentTable = new BASheet('companyContent', $contactActivity);
            $contentView = $contentTable->getView();
            //如果添加了多个companycontent，只获取最后一个
            $contactList = end($contentView);
        }
        
        $this->updateControl->addRenderView('contactus', 'contactus', array('contactList' => $contactList));
    }

    /**
     * 内容处理类方法
     */
    public function onShowActivityList() { //跳转活动列表
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);

        $activityTable = new BASheet('companyActivity', $event);
        $activityView = $activityTable->getView();

        $this->updateControl->addRenderView('activity_list', 'activity_list', array(
            'activityView' => $activityView,
            'eventId' => $eventId));
    }

    public function onShowAddCompanyActivity() {//跳转添加页面
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');

        $model = new BDynamicFormModel();
        $activityNameField = 'media_name_add_activity';
        $activityDescriptionField = 'media_description_add_activity';
        $activityIdentityField = 'media_identity_add_activity';

        $model->addField($activityNameField);
        $model->addField($activityDescriptionField);
        $model->addField($activityIdentityField);

        if ($activityId != NULL && $activityId > 0) {
            $eventTable = new BAEventSheet();
            $event = $eventTable->getListById($eventId);

            $activityTable = new BASheet('companyActivity', $event);
            $activity = $activityTable->getListById($activityId);

            //将已经存在的活动数据放进MODEL里面
            $model->$activityNameField = $activity->getItem('name')->getValue();
            if ($activity->getItem('description') != NULL)
                $model->$activityDescriptionField = $activity->getItem('description')->getValue();
            if ($activity->getItem('identity') != NULL)
                $model->$activityIdentityField = $activity->getItem('identity')->getValue();
        }

        $this->updateControl->addRenderView('activityAdd', 'activity_add', array(
            'model'=>$model,
            'activityNameField'=>$activityNameField,
            'activityIdentityField'=>$activityIdentityField,
            'activityDescriptionField'=>$activityDescriptionField,
            'eventId' => $eventId,
            'activityId' => $activityId));
    }
    public function onAddCompanyActivity() { //添加活动activity
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');
        //$contentId = $this->activeLink->getLinkVariable('contentId');
        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);

        $activityTable = new BASheet('companyActivity', $event);

        if ($activityId != NULL && $activityId > 0) {
            $activity = $activityTable->getListById($activityId);
        } else {
            $activity = $activityTable->getNewList();
        }

        $mediaList = $this->postManager->populateMediaListFromPostData($this->postData, 'add_activity');

        $itemData = array();
        foreach ($mediaList as $media) {
            $name = $media->name;
            $value = $media->content;
            $itemData[$name] = $value;
        }

        $activity->addItems($itemData);
        
        $this->onShowActivityList();

        //$this->updateControl->addRenderView('activity_list', 'activity_list', array('eventId' => $eventId));
    }


    public function onShowCompanyContent() { //跳转内容列表
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');
        
        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);
        $activityTable = new BASheet('companyActivity',$event);
        $activity = $activityTable->getListById($activityId);

        $contentTable = new BASheet('companyContent', $activity);
        $contentView = $contentTable->getView();

        $this->updateControl->addRenderView('content_list', 'content_list', array(
            'contentView' => $contentView,
            'activityId'=>$activityId,
            'eventId' => $eventId));
    }

    public function onShowAddCompanyContent() { //跳转内容添加
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');
        $contentId = $this->activeLink->getLinkVariable('contentId');

        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);
        $model = new BDynamicFormModel();
        $contentNameField = 'media_name_add_content';
        $contentDescriptionField = 'media_description_add_content';
        $contentAuthorField = 'media_author_add_content';
        $contentTimeField = 'media_time_add_content';
        $contentImageField = 'image_media_image_add_content';
        $model->addField($contentNameField);
        $model->addField($contentDescriptionField);
        $model->addField($contentAuthorField);
        $model->addField($contentTimeField);
        $model->addField($contentImageField);

        if ($contentId != NULL && $contentId > 0) {
          //  $activityTable = new BASheet();
            $activityTable = new BASheet('companyActivity',$event);

            $activity = $activityTable->getListById($activityId);

            $contentTable = new BASheet('companyContent', $activity);
            $content = $contentTable->getListById($contentId);

            //将已经存在的活动数据放进MODEL里面
            $model->$contentNameField = $content->getItem('name')->getValue();
            if ($content->getItem('description') != NULL)
            {
                $model->$contentDescriptionField = $content->getItem('description')->getValue();
            }
            if ($content->getItem('author') != NULL){
                $model->$contentAuthorField = $content->getItem('author')->getValue();
            }
            if ($content->getItem('time') != NULL){
                $model->$contentTimeField = $content->getItem('time')->getValue();
            }
            if ($content->getItem('image') != NULL){
                $model->$contentImageField = $content->getItem('image')->getValue();
            }
        }

        $this->updateControl->addRenderView('contentAdd', 'content_add', array(
            'model'=>$model,
            'contentNameField'=>$contentNameField,
            'contentDescriptionField'=>$contentDescriptionField,
            'contentAuthorField'=>$contentAuthorField,
            'contentTimeField'=>$contentTimeField,
            'contentImageField'=>$contentImageField,
            'activityId' => $activityId,
            'eventId'=>$eventId,
            'contentId'=>$contentId));
    }

    public function onAddCompanyContent() { //添加content
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');
        $contentId = $this->activeLink->getLinkVariable('contentId');
        
        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);
        $activityTable = new BASheet('companyActivity',$event);
        $activity = $activityTable->getListById($activityId);
       
        $contentTable = new BASheet('companyContent', $activity);
        if ($contentId != NULL && $contentId > 0) {
            $content = $contentTable->getListById($contentId);
        } else {
            $content = $contentTable->getNewList();
        }
        $mediaList = $this->postManager->populateMediaListFromPostData($this->postData, 'add_content');
        $itemData = array();
        foreach ($mediaList as $media) {
            $name = $media->name;
            $value = $media->content;
            $itemData[$name] = $value;
        }
        $content->addItems($itemData);
        $this->onShowCompanyContent();
    }
    
    public function onDelCompanyActivity() {
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');
        if ($activityId != NULL) {
            $eventTable = new BAEventSheet();
            $event = $eventTable->getListById($eventId);
            $activityTable = new BASheet('companyActivity', $event);
            $activityTable->deleteList($activityId);
        }
        $this->onShowActivityList();
    }
    public function onDelCompanyContent() {
        $eventId = $this->activeLink->getLinkVariable('eventId');
        $activityId = $this->activeLink->getLinkVariable('activityId');
        $contentId = $this->activeLink->getLinkVariable('contentId');
        if ($contentId != NULL) {
            $eventTable = new BAEventSheet();
            $event = $eventTable->getListById($eventId);
            $activityTable = new BASheet('companyActivity', $event);
            $activity = $activityTable->getListById($activityId);
            $contentTable = new BASheet('companyContent', $activity);
            $contentTable->deleteList($contentId);
        }
        $this->onShowCompanyContent();
    }
}
